{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "governmental-swaziland",
   "metadata": {},
   "source": [
    "# Canadian Business Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "offensive-vegetation",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "durable-destruction",
   "metadata": {},
   "source": [
    "The function `clean_ca_bn()` cleans a column containing Canadian Business Number (BN) strings, and standardizes them in a given format. The function `validate_ca_bn()` validates either a single BN strings, a column of BN strings or a DataFrame of BN strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "noticed-journey",
   "metadata": {},
   "source": [
    "BN strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"123026635\"\n",
    "* `standard`: BN strings with proper whitespace in the proper places. Note: in the case of BN, the compact format is the same as the standard one.\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_ca_bn()` and `validate_ca_bn()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "historic-auditor",
   "metadata": {},
   "source": [
    "### An example dataset containing BN strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "changing-revolution",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"bn\": [\n",
    "            \"12302 6635\",\n",
    "            \"12302 6635 RC 0001\",\n",
    "            \"12345678Z\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "increasing-usage",
   "metadata": {},
   "source": [
    "## 1. Default `clean_ca_bn`\n",
    "\n",
    "By default, `clean_ca_bn` will clean bn strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "moving-mirror",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_ca_bn\n",
    "clean_ca_bn(df, column = \"bn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aware-theory",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "latter-macedonia",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tired-optics",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "early-springfield",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ca_bn(df, column = \"bn\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "satisfactory-oregon",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "driven-textbook",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ca_bn(df, column = \"bn\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "subject-installation",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned BN strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "lovely-license",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ca_bn(df, column=\"bn\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "classified-reputation",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "related-regular",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fifty-protest",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ca_bn(df, \"bn\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "western-helping",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "accepting-mozambique",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ca_bn(df, \"bn\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "reduced-junior",
   "metadata": {},
   "source": [
    "## 4. `validate_ca_bn()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "living-waste",
   "metadata": {},
   "source": [
    "`validate_ca_bn()` returns `True` when the input is a valid BN. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_ca_bn()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_ca_bn()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_ca_bn()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "junior-wellington",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_ca_bn\n",
    "print(validate_ca_bn(\"12302 6635\"))\n",
    "print(validate_ca_bn(\"12302 6635 RC 0001\"))\n",
    "print(validate_ca_bn(\"12345678Z\"))\n",
    "print(validate_ca_bn(\"51 824 753 556\"))\n",
    "print(validate_ca_bn(\"hello\"))\n",
    "print(validate_ca_bn(np.nan))\n",
    "print(validate_ca_bn(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "molecular-suicide",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "transparent-mambo",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_ca_bn(df[\"bn\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bound-alpha",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abandoned-impact",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_ca_bn(df, column=\"bn\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "blank-cross",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "expanded-watershed",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_ca_bn(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "biological-bundle",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
